package com.example.safeassistan.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.my.fragment.FeedBackFragment;
import java.util.ArrayList;
import java.util.List;


public class FeedbackListActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvUnHandle,tvHandled;
    private List<String> mTitleList; //Tab的标题
    private List<Fragment> fragmentList;
    private ViewPager viewpager;
    private MainAdapter adapter;
    private ImageView ivBack;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, FeedbackListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_feedbacklist;
    }

    @Override
    protected void initView() {
        viewpager=findViewById(R.id.viewpager);
        tvUnHandle=findViewById(R.id.tvUnHandle);
        tvUnHandle.setOnClickListener(this);
        tvHandled=findViewById(R.id.tvHandled);
        ivBack=findViewById(R.id.ivBack);
        tvHandled.setOnClickListener(this::onClick);
        ivBack.setOnClickListener(this::onClick);

        mTitleList=new ArrayList<>();
        mTitleList.add("未处理");
        mTitleList.add("已处理");
        fragmentList=new ArrayList<>();
        fragmentList.add(FeedBackFragment.newInstance(0));
        fragmentList.add(FeedBackFragment.newInstance(1));
        adapter=new MainAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        reSetStatus();
                        tvUnHandle.setBackground(getResources().getDrawable(R.drawable.leftcorner_blue_bg_5));
                        tvUnHandle.setTextColor(getResources().getColor(R.color.white));
                        break;
                    case 1:
                        reSetStatus();
                        tvHandled.setBackground(getResources().getDrawable(R.drawable.rightcorner_blue_bg));
                        tvHandled.setTextColor(getResources().getColor(R.color.white));
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvUnHandle:
                viewpager.setCurrentItem(0);
                break;
            case R.id.tvHandled:
                viewpager.setCurrentItem(1);
                break;
            case R.id.ivBack:
                finish();
                break;
        }
    }

    public void reSetStatus(){
        tvUnHandle.setBackground(null);
        tvHandled.setBackground(null);
        tvUnHandle.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
        tvHandled.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
    }
    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mTitleList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }
}